
****************************************************************************************************************************
********** THIS .DO FILE PERFORMS A ROBUSTNESS TEST USING DIFFERENCED FISCAL OUTCOMES (RATHER THAN TERM-AVERAGES) **********
****************************************************************************************************************************


clear *

use "${root}/data/processed/final_sample.dta", clear


************************************************************************************
** Perform (robust bias-corrected) RD estimates using differenced fiscal outcomes **
************************************************************************************

foreach outcome in 	tot_exp_change_pc 		 	///
					tot_exp_change_sgdp 	 	///
					tot_rev_change_pc 		 	///
					tot_rev_change_sgdp 	 	///
					current_exp_change_share 	///
					investments_change_share 	///
					personnel_change_share 	 	///
					social_exp_change_share  	///
					social_exp_change_pc 	 	///
					health_change_share 	 	///
					education_change_share 	 	///
					welfare_change_share 	 	///
					nonsocial_exp_change_share 	///
					housing_change_share 		///
					transport_change_share 		///
					other_change_share	{
						
						*** residualize on year effects
						qui reg `outcome' yy2 yy3
						qui predict yyres_`outcome', residuals
						
						*** (1) baseline (whole sample)
						qui rdrobust yyres_`outcome' margin_mayor_left if baseline_sample==1, vce(cluster mun_code) all

						local b1_`outcome' : di %9.2f (e(tau_bc))
						local se1_`outcome' : di %9.2f (e(se_tau_rb))
						local pv1_`outcome' : di %9.2f (e(pv_rb))
						local n1_`outcome' = e(N)
						local en1_`outcome' = e(N_h_l) + e(N_h_r)
	
						*** (2) lame-ducks subsample

						qui rdrobust yyres_`outcome' margin_mayor_left if baseline_sample==1 & lame_duck==1, vce(cluster mun_code) all

						local b2_`outcome' : di %9.2f (e(tau_bc))
						local se2_`outcome' : di %9.2f (e(se_tau_rb))
						local pv2_`outcome' : di %9.2f (e(pv_rb))
						local n2_`outcome' = e(N)
						local en2_`outcome' = e(N_h_l) + e(N_h_r)
	

						*** (3) Tiebout-below-median subsample

						qui rdrobust yyres_`outcome' margin_mayor_left if baseline_sample==1 & tiebout_median_sample == 1, vce(cluster mun_code) all
	
						local b3_`outcome' : di %9.2f (e(tau_bc))
						local se3_`outcome' : di %9.2f (e(se_tau_rb))
						local pv3_`outcome' : di %9.2f (e(pv_rb))
						local n3_`outcome' = e(N)
						local en3_`outcome' = e(N_h_l) + e(N_h_r)
	
						*** (4) Coalition distance median (4-years average)

						qui rdrobust yyres_`outcome' margin_mayor_left if baseline_sample==1 & coal_dist_median_sample == 1, vce(cluster mun_code) all
	
						local b4_`outcome' : di %9.2f (e(tau_bc))
						local se4_`outcome' : di %9.2f (e(se_tau_rb))
						local pv4_`outcome' : di %9.2f (e(pv_rb))
						local n4_`outcome' = e(N)
						local en4_`outcome' = e(N_h_l) + e(N_h_r)

						*** (5) Oil-windfalls subsample (4-years average)

						qui rdrobust yyres_`outcome' margin_mayor_left if baseline_sample==1 & oil_sample == 1, vce(cluster mun_code) all
	
						local b5_`outcome' : di %9.2f (e(tau_bc))
						local se5_`outcome' : di %9.2f (e(se_tau_rb))
						local pv5_`outcome' : di %9.2f (e(pv_rb))
						local n5_`outcome' = e(N)
						local en5_`outcome' = e(N_h_l) + e(N_h_r)
	
				}
				
				
*************************************
** Export results in a Latex table **
*************************************

texdoc init "${root}/results/tables/robust_differences_table.tex", replace force

tex \caption{RD estimates of the effect of a left-wing mayor, using differenced outcomes}

tex \resizebox{\linewidth}{!}{
tex \begin{tabularx}{\linewidth}{l *5{>{\Centering}X}}
tex \toprule

tex 													&        Baseline 				& \multicolumn{4}{c}{Subsamples} 								\\
tex \cmidrule(lr){2-2} \cmidrule(lr){3-6}

tex 													& 								& 	Lame Duck 			& Tiebout $<$ median	 								& Ideology distance $>$ median  						& Oil windfall					\\
tex \midrule

tex \multicolumn{6}{c}{Size of government: overall revenues and expenses} \\
tex \midrule
tex Expenditure per capita 					& `b1_tot_exp_change_pc'			& `b2_tot_exp_change_pc'				& `b3_tot_exp_change_pc' 				& `b4_tot_exp_change_pc'			& `b5_tot_exp_change_pc'			\\
tex  										& (`se1_tot_exp_change_pc') 		& (`se2_tot_exp_change_pc') 			& (`se3_tot_exp_change_pc') 			& (`se4_tot_exp_change_pc') 		& (`se5_tot_exp_change_pc') 		\\
tex Expenditure, \% of GDP 					& `b1_tot_exp_change_sgdp'			& `b2_tot_exp_change_sgdp'				& `b3_tot_exp_change_sgdp'	 			& `b4_tot_exp_change_sgdp'	 		& `b5_tot_exp_change_sgdp'	 		\\
tex  										& (`se1_tot_exp_change_sgdp') 		& (`se2_tot_exp_change_sgdp') 			& (`se3_tot_exp_change_sgdp') 			& (`se4_tot_exp_change_sgdp') 		& (`se5_tot_exp_change_sgdp') 		\\
tex Revenue per capita 						& `b1_tot_rev_change_pc'			& `b2_tot_rev_change_pc'				& `b3_tot_rev_change_pc'				& `b4_tot_rev_change_pc'			& `b5_tot_rev_change_pc'			\\
tex  										& (`se1_tot_rev_change_pc') 		& (`se2_tot_rev_change_pc') 			& (`se3_tot_rev_change_pc') 			& (`se4_tot_rev_change_pc') 		& (`se5_tot_rev_change_pc') 		\\
tex Revenue, \% of GDP 						& `b1_tot_rev_change_sgdp'			& `b2_tot_rev_change_sgdp'				& `b3_tot_rev_change_sgdp'				& `b4_tot_rev_change_sgdp'			& `b5_tot_rev_change_sgdp'			\\
tex  										& (`se1_tot_rev_change_sgdp') 		& (`se2_tot_rev_change_sgdp') 			& (`se3_tot_rev_change_sgdp') 			& (`se4_tot_rev_change_sgdp') 		& (`se5_tot_rev_change_sgdp') 		\\
tex \midrule
tex \multicolumn{6}{c}{Allocation of resources: budget categories (\% of total expenditure)} \\
tex \midrule
tex Current Expenditure 					& `b1_current_exp_change_share'		& `b2_current_exp_change_share'			& `b3_current_exp_change_share'			& `b4_current_exp_change_share'		& `b5_current_exp_change_share'		\\
tex  										& (`se1_current_exp_change_share') 	& (`se2_current_exp_change_share') 		& (`se3_current_exp_change_share') 		& (`se4_current_exp_change_share') 	& (`se5_current_exp_change_share') 	\\
tex \multicolumn{6}{l}{of which:} \\
tex \hspace{0.20cm} Personnel 								& `b1_personnel_change_share'		& `b2_personnel_change_share'			& `b3_personnel_change_share'			& `b4_personnel_change_share'		& `b5_personnel_change_share'		\\
tex  										& (`se1_personnel_change_share') 	& (`se2_personnel_change_share') 		& (`se3_personnel_change_share') 		& (`se4_personnel_change_share') 	& (`se5_personnel_change_share') 	\\
tex Public Investment 						& `b1_investments_change_share'		& `b2_investments_change_share'			& `b3_investments_change_share' 		& `b4_investments_change_share' 	& `b5_investments_change_share'		\\
tex  										& (`se1_investments_change_share') 	& (`se2_investments_change_share') 		& (`se3_investments_change_share') 		& (`se4_investments_change_share') 	& (`se5_investments_change_share') 	\\
tex \midrule
tex \multicolumn{6}{c}{Allocation of resources: functional categories (\% of total expenditure)} \\
tex \midrule
tex Social Expenditures 					& `b1_social_exp_change_share' 		& `b2_social_exp_change_share'			& `b3_social_exp_change_share' 			& `b4_social_exp_change_share'		& `b5_social_exp_change_share'		\\
tex  										& (`se1_social_exp_change_share') 	& (`se2_social_exp_change_share') 		& (`se3_social_exp_change_share') 		& (`se4_social_exp_change_share') 	& (`se5_social_exp_change_share') 	\\
tex \multicolumn{6}{l}{of which:} \\
tex \hspace{0.20cm} Health \& sanitation 	& `b1_health_change_share'			& `b2_health_change_share'				& `b3_health_change_share'				& `b4_health_change_share' 			& `b5_health_change_share' 			\\
tex  										& (`se1_health_change_share') 		& (`se2_health_change_share') 			& (`se3_health_change_share') 			& (`se4_health_change_share') 		& (`se5_health_change_share') 		\\
tex \hspace{0.20cm} Education \& culture 	& `b1_education_change_share'		& `b2_education_change_share' 			& `b3_education_change_share'			& `b4_education_change_share'		& `b5_education_change_share'		\\
tex  										& (`se1_education_change_share') 	& (`se2_education_change_share') 		& (`se3_education_change_share') 		& (`se4_education_change_share') 	& (`se5_education_change_share') 	\\
tex \hspace{0.20cm} Social welfare 			& `b1_welfare_change_share'			& `b2_welfare_change_share'				& `b3_welfare_change_share'				& `b4_welfare_change_share'			& `b5_welfare_change_share'			\\
tex  										& (`se1_welfare_change_share') 		& (`se2_welfare_change_share') 			& (`se3_welfare_change_share') 			& (`se4_welfare_change_share') 		& (`se5_welfare_change_share') 		\\
tex \multicolumn{6}{l}{Other expenditures:} \\
tex  \hspace{0.20cm} Housing 				& `b1_housing_change_share'			& `b2_housing_change_share'				& `b3_housing_change_share'				& `b4_housing_change_share'			& `b5_housing_change_share'			\\
tex  										& (`se1_housing_change_share') 		& (`se2_housing_change_share') 			& (`se3_housing_change_share') 			& (`se4_housing_change_share') 		& (`se5_housing_change_share') 		\\
tex  \hspace{0.20cm} Transportation 		& `b1_transport_change_share'		& `b2_transport_change_share'			& `b3_transport_change_share'			& `b4_transport_change_share'		& `b5_transport_change_share'		\\
tex  										& (`se1_transport_change_share') 	& (`se2_transport_change_share') 		& (`se3_transport_change_share') 		& (`se4_transport_change_share') 	& (`se5_transport_change_share') 	\\
tex  \hspace{0.20cm} Other 					& `b1_other_change_share'			& `b2_other_change_share'				& `b3_other_change_share'				& `b4_other_change_share'			& `b5_other_change_share'			\\
tex  										& (`se1_other_change_share') 		& (`se2_other_change_share') 			& (`se3_other_change_share') 			& (`se4_other_change_share') 		& (`se5_other_change_share') 		\\
tex \midrule
tex Social Expenditures per capita 			& `b1_social_exp_change_pc'			& `b2_social_exp_change_pc'				& `b3_social_exp_change_pc'				& `b4_social_exp_change_pc'			& `b5_social_exp_change_pc'			\\
tex  										& (`se1_social_exp_change_pc') 		& (`se2_social_exp_change_pc') 			& (`se3_social_exp_change_pc') 			& (`se4_social_exp_change_pc') 		& (`se5_social_exp_change_pc') 		\\
tex \bottomrule
tex Observations (all) 						&  `n1_tot_exp_change_pc'    		& `n2_tot_exp_change_pc'  				& `n3_tot_exp_change_pc'  				& `n4_tot_exp_change_pc'  			& `n5_tot_exp_change_pc'  			\\
tex Observations (effective)				&  `en1_tot_exp_change_pc'			& `en2_tot_exp_change_pc'				& `en3_tot_exp_change_pc'   			& `en4_tot_exp_change_pc'			& `en5_tot_exp_change_pc'   		\\
tex \bottomrule
tex \end{tabularx}}

texdoc close
